package com.xcm.service.impl;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.xcm.constant.CacheConstant;
import com.xcm.core.cache.redis.RedisCacheDao;
import com.xcm.model.TbDeviceInfo;
import com.xcm.model.TbDeviceLog;
import com.xcm.model.TbProgramInfo;
import com.xcm.model.bo.ClientDeviceBO;
import com.xcm.service.ClientService;
import com.xcm.service.DeviceInfoService;
import com.xcm.service.DeviceLogService;
import com.xcm.service.ProgramDownloadService;
import com.xcm.service.ProgramInfoService;
import com.xcm.util.DateTools;

/**
 * 客户端与服务端交互逻辑Service实现类
 */
@Service(value = "clientService")
public class ClientServiceImpl implements ClientService {
	@Autowired
	private RedisCacheDao redisCacheDao;
	@Autowired
	private DeviceInfoService deviceInfoService;
	@Autowired
	private ProgramInfoService programInfoService;
	@Autowired
	private ProgramDownloadService programDownloadService;
	@Autowired
	private DeviceLogService deviceLogService;

	/**
	 * 获取最新设置数据
	 *
	 * @param mac     mac地址
	 * @param xcVersion 版本号
	 * @param clientUPdeviceNum 客户端当前上传的机器码
	 * @return ClientDeviceBO
	 */
	@Override
	@Transactional(readOnly = false, rollbackFor = Exception.class)
	public ClientDeviceBO getStates(String mac, String xcVersion,TbDeviceInfo deviceInfo) {
		//上线判断
		Object cache = redisCacheDao.getCache(CacheConstant.CACHE_NAME_DEVICE_ONLINE, mac);
		//没有登陆数据
		if (cache == null) {
			//设置上线
			deviceInfo.setDeviceStatus(0);
		}
		//放入缓存
		redisCacheDao.putCache(CacheConstant.CACHE_NAME_DEVICE_ONLINE, mac, System.currentTimeMillis() / 1000);
		ClientDeviceBO resultClientDeviceBO = new ClientDeviceBO();
		resultClientDeviceBO.setDeviceVolume(deviceInfo.getDeviceVolume());
		
		// 使用了定时开关机
		if (deviceInfo.getUseTimer() != null &&  1 == deviceInfo.getUseTimer() && deviceInfo.getBootTime() != null && deviceInfo.getOffTime() != null) {
			resultClientDeviceBO.setBootTime(DateTools.longTimeStampToStringDate(deviceInfo.getBootTime(), "HH:mm"));
			resultClientDeviceBO.setOffTime(DateTools.longTimeStampToStringDate(deviceInfo.getOffTime(), "HH:mm"));
		}
		
		//判断是否有开机关机设置
		ClientDeviceBO optionClientDevice = (ClientDeviceBO) redisCacheDao.getCache(CacheConstant.CACHE_NAME_DEVICE_OPTION, mac);
		resultClientDeviceBO.setOff(false);
		resultClientDeviceBO.setRestart(false);
		resultClientDeviceBO.setLog(false);
		if (optionClientDevice != null) {
			if ( StringUtils.isNotBlank(optionClientDevice.getDeviceNum()) ) {
				resultClientDeviceBO.setDeviceNum(optionClientDevice.getDeviceNum());
			}
			if (optionClientDevice.getOff() != null) {
				resultClientDeviceBO.setOff(optionClientDevice.getOff());
			}
			if (optionClientDevice.getRestart() != null) {
				resultClientDeviceBO.setRestart(optionClientDevice.getRestart());
			}
			if (optionClientDevice.getLog() != null) {
				resultClientDeviceBO.setLog(optionClientDevice.getLog());
			}
			redisCacheDao.deleteByKey(CacheConstant.CACHE_NAME_DEVICE_OPTION, mac);
		}
		
		// 通过机器码查询节目信息
		TbProgramInfo programInfo = programInfoService.getProgramInfoByMachineNum(deviceInfo.getDeviceNum(),null);
		if (programInfo != null && ( StringUtils.isBlank(xcVersion) || !xcVersion.equals(programInfo.getXcVersion()) )) {
			resultClientDeviceBO.setNewProgramXcVersion(programInfo.getXcVersion());
		}
		
		//	一次性使用设置
		deviceInfo.setDeviceVolume(null);
		deviceInfo.setUseTimer(null);
		deviceInfo.setBootTime(null);
		deviceInfo.setOffTime(null);
		deviceInfoService.updateDeviceInfo(deviceInfo);
		return resultClientDeviceBO;
	}

	/**
	 * 上报广告数据的下载状态
	 *
	 * @param mac          mac地址
	 * @param playJson     当前播放Json
	 * @param downloadJson 当前下载
	 * @param state        下载状态
	 * @param failure_reasonon 失败原因
	 * @param ip 请求ip
	 */
	@Override
	public void setDownload(String mac, String playJson, String downloadJson, Integer state,String failure_reasonon,String ip) {
		programDownloadService.saveOrUpdateDownload(mac,playJson,downloadJson,state,failure_reasonon,ip);
	}

	/**
	 * 上传日志
	 * @param mac   mac地址
	 * @param name  压缩包名
	 * @param url   文件地址
	 * @param type 日志类型 @see com.xcm.constant.LogType
	 */
	@Override
	@Transactional(readOnly = false, rollbackFor = Exception.class)
	public void uploadLog(String mac, String name, String url,String type) {
		String[] urls = url.split(",");
		if ( urls.length > 0 ) {
			TbDeviceLog deviceLog ;
			for ( String u : urls ) {
				deviceLog = new TbDeviceLog();
				deviceLog.setMac(mac);
				deviceLog.setName(name);
				deviceLog.setUrl(u);
				deviceLog.setType(type);
				deviceLog.setUploadTime(System.currentTimeMillis());
				deviceLogService.saveDeviceLog(deviceLog);
			}
		}
		
	}
	
}
